package com.klhappy.houyuan.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Environment;

import com.klhappy.houyuan.constant.KeyConstant;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileUtils {
	public static String photoUrl;
	public static String SDPATH = Environment.getExternalStorageDirectory() + File.separator + KeyConstant.APP_DIR
			+ File.separator + KeyConstant.IMAGE_DIR + File.separator
			+ KeyConstant.HEAD_SHOT;
	public static String IMAGEPATH = Environment.getExternalStorageDirectory() + File.separator + KeyConstant.APP_DIR
			+ File.separator + KeyConstant.IMAGE_DIR + File.separator
			+ KeyConstant.HEAD_SHOT;
	
	public static void saveBitmap(Bitmap bm, String picName) {
		try {
			if (!isFileExist("")) {
				File tempf = createSDDir("");
			}
			File f = new File(SDPATH, picName + ".jpg");
			photoUrl=SDPATH + picName + ".jpg";
			if (f.exists()) {
				f.delete();
			}
			FileOutputStream out = new FileOutputStream(f);
			bm.compress(Bitmap.CompressFormat.PNG, 90, out);
			out.flush();
			out.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static File createSDDir(String dirName) throws IOException {
		File dir = new File(SDPATH + dirName);
		if (Environment.getExternalStorageState().equals(
				Environment.MEDIA_MOUNTED)) {

			System.out.println("createSDDir:" + dir.getAbsolutePath());
			System.out.println("createSDDir:" + dir.mkdir());
		}
		return dir;
	}

	public static boolean isFileExist(String fileName) {
		File file = new File(SDPATH + fileName);
		file.isFile();
		return file.exists();
	}
	
	public static void delFile(String fileName){
		File file = new File(SDPATH + fileName);
		if(file.isFile()){
			file.delete();
        }
		file.exists();
	}

	public static void deleteDir() {
		File dir = new File(SDPATH);
		if (dir == null || !dir.exists() || !dir.isDirectory())
			return;
		
		for (File file : dir.listFiles()) {
			if (file.isFile())
				file.delete(); 
			else if (file.isDirectory())
				deleteDir(); 
		}
		dir.delete();
	}

	public static boolean fileIsExists(String path) {
		try {
			File f = new File(path);
			if (!f.exists()) {
				return false;
			}
		} catch (Exception e) {

			return false;
		}
		return true;
	}
	
	/**
	 * 检查存储卡是否插入
	 * @return
	 */
	public static boolean hasSdcard() {
	    String status = Environment.getExternalStorageState();
	    if (status.equals(Environment.MEDIA_MOUNTED)){
	       return true;
	    } else {
	       return false;
	    }
	}
	
	/**
	 * 获取图片存储目录
	 * 
	 * 有SD卡，则保存在SD卡；没有，则保存在应用目录下
	 */
	public static String getImagePath(Context context) {
		String path = "";
		if (hasSdcard()) {
			path = IMAGEPATH;
		}else {
			path = context.getApplicationContext().getFilesDir().getPath() + File.separator + KeyConstant.APP_DIR
					+ File.separator + KeyConstant.IMAGE_DIR + File.separator
					+ KeyConstant.HEAD_SHOT;
		}
		File file = new File(path);
		if (!file.exists() || !file.isDirectory()) {
			file.mkdirs();
		}
		return path;
	}
	
	/**
	 * 获取本应用临时存储目录，发送图片时使用
	 * 
	 */
	public static String getImageCatch(Context context) {
		String path = context.getApplicationContext().getFilesDir().getPath() + File.separator + KeyConstant.APP_DIR
				+ File.separator + KeyConstant.IMAGE_DIR + File.separator
				+ KeyConstant.HEAD_SHOT;
		File file = new File(path);
		if (!file.exists() || !file.isDirectory()) {
			file.mkdirs();
		}
		return path;
	}
	
	/**
	 * 清空本应用临时存储目录，发送图片时使用
	 * 
	 */
	public static void clearImageCatch(Context context) {
		String path = context.getApplicationContext().getFilesDir().getPath() + File.separator + KeyConstant.APP_DIR
				+ File.separator + KeyConstant.IMAGE_DIR + File.separator
				+ KeyConstant.HEAD_SHOT;
		File file = new File(path);
		if (!file.exists() || !file.isDirectory()) {
			return;
		}
		File[] files = file.listFiles();
		if (null == files || files.length <= 0) {
			return;
		}
		for(int i = 0; i < files.length; i++) {
			if(files[i].isFile()) {
				files[i].delete();
			}
		}
	}

	/**
	 * 获取文件大小
	 * 
	 * @param file 源文件
	 * 
	 * @return 文件大小，size/1000为KB。文件不存在或是文件夹，返回0
	 */
	public static int getFileSize(File file) {
		if (null == file || !file.exists() || !file.isFile()) {
			return 0;
		}
		int size = 0;
		FileInputStream fis = null;
		try {
			fis = new FileInputStream(file);
			size = fis.available();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return size;
	}

}
